﻿/*
Copyright (C) 2008 Stephen Packer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var 	visits_ever_total 		= 0;
var 	visits_ever_unique 		= 0;
var 	visits_today_total 		= 0;
var 	visits_today_unique 	= 0;
var 	visits_hour_total 		= 0;
var 	visits_hour_unique 	    = 0;
var     fade_timer              = 250;  // quarter of a second
var     updateTimer;                    // object placeholder
var     rotateTimer;                    // timer placeholder for rotating arrows
var     errorMsg                = "";   // error message placeholder

fetch_data();    
updateTimer = setInterval("fetch_data()", 1000 * 60 * options.getValue("updateFrequency"));

function view_onOpen() {
    setOpacity( options.getValue("Opacity") );
    plugin.onAddCustomMenuItems = onAddCustomMenuItems; // function to add items to the right-click menu
}


function onAddCustomMenuItems(menu) {
    menu.AddItem(MENU_FORCE_REFRESH, 0, fetch_data);
    // Opacity
    var opacSub = menu.AddPopup(MENU_OPACITY);
    for (var i=100; i>10; i-=10) {
        var capt = i+"%";
        opacSub.AddItem(capt, (getOpacity()==i)?gddMenuItemFlagChecked:0, onOpacityClicked);
    }
}

function rotateDivs() {
    var div = [divEver, divToday, divHour];
    var visible = 0;
    var ondeck  = 0;
    // iterate once to find visible div & mark as hidden
    for (var i=0; i<div.length; i++) {
        if (div[i].opacity == 255) {
            visible = i;
            beginAnimation("fade("+i+")", 255, 0, fade_timer);
        }
    }
    if (div[visible+1])
        ondeck = visible + 1;
    beginAnimation("fade("+ondeck+")", 0, 255, fade_timer);
    
    
    if (div[ondeck].name == 'divToday')
        options.putValue("captionPref", 'Today');
    else if (div[ondeck].name == 'divHour')
        options.putValue("captionPref", 'Hour');
    else
        options.putValue("captionPref", 'Ever');

    update_caption();
}
function fade(i) {
    var div = [divEver, divToday, divHour];
    div[i].opacity = event.value;
}

function alerter(sMessage, sTitle) {
    errorMsg = sMessage;
    clearInterval(rotateTimer);
    imgRefresh.rotation = 0;
    imgRefresh.src = "alert.png";
    imgRefresh.onclick = function(){ alert(errorMsg); };
    return;
}
function alerter_enabled() {
    options.putValue("alerter_enabled", true);
    return true;
}